#include "StoneGame.h"

CStoneGame::CStoneGame(void)
{
}

CStoneGame::~CStoneGame(void)
{
}

HandType CStoneGame::PK( HandType eLeft,HandType eRight )
{
	if (eLeft == HT_NULL || eRight == HT_NULL)
	{
		return HT_NULL;
	}

	if (eLeft == eRight)
	{
		return HT_NULL;
	}

	typedef std::pair<HandType,HandType> PkUnit;
	std::vector<PkUnit> akUnit;
	akUnit.push_back(PkUnit(HT_STONE,HT_CLIPPER));
	akUnit.push_back(PkUnit(HT_CLIPPER,HT_CLOTH));
	akUnit.push_back(PkUnit(HT_CLOTH,HT_STONE));

	for (int i = 0;i < akUnit.size();i++)
	{
		const PkUnit& rkUnit = akUnit[i];
		if (eLeft == rkUnit.first)
		{
			return (eRight == rkUnit.second ? eLeft : eRight);
		}
	}

	return HT_NULL;
}

std::map<HandType,std::vector<int>> CStoneGame::CalResult( const std::vector<HandType>& raiTypes )
{
	std::map<HandType,std::vector<int>> kResult;
	for (int i = 0;i < raiTypes.size();i++)
	{
		kResult[raiTypes[i]].push_back(i);
	}

	if (kResult.size() == 1 || kResult.size() == 3)
	{
		return std::map<HandType,std::vector<int>>();
	}

	if (kResult.size() != 2)
	{
		_ASSERT(0);
		return std::map<HandType,std::vector<int>>();
	}

	std::map<HandType,std::vector<int>>::iterator kIter = kResult.begin();
	std::map<HandType,std::vector<int>>::iterator kIter1 = kIter;
	std::map<HandType,std::vector<int>>::iterator kIter2 = ++kIter;

	HandType eType = PK(kIter1->first,kIter2->first);
	if (eType == HT_NULL)
	{
		_ASSERT(0);
		return std::map<HandType,std::vector<int>>(); 
	}

	std::vector<int> aiTypes = kResult[eType];
	kResult.clear();
	
	kResult[eType] = aiTypes;
	return kResult;
}
